package org.aptech.hdax.easybuy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.aptech.hdax.easybuy.pojo.Product;
import org.aptech.hdax.easybuy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/list", produces = {"application/json;charset=utf-8"})
    public Map<String,Object> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size, Product condition) throws Exception {
        Page<Product> pager = PageHelper.startPage(page, size);
        List<Product> list = productService.findAll(condition);

        Map<String,Object> map = new HashMap<>();
        map.put("total",pager.getTotal());
        map.put("rows",list);

        return map;
    }

    @RequestMapping("/view/{id}")
    public Product view(@PathVariable("id") Integer id) throws Exception {
        return productService.get(id);
    }
}
